﻿package com.dykj.apps.web.core;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.struts2.ServletActionContext;
import com.dykj.platform.framework.common.paging.Page;
import com.dykj.platform.framework.common.paging.UIHelper;
import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.ModelDriven;

@SuppressWarnings("serial")
public abstract class BaseAction extends ActionSupport implements ModelDriven{

	public String jsonString;//返回JSON的字符串
	private String msg;		//提示信息
	private Map<Object, Object> requestMsg = new HashMap<Object, Object>(); //放进request中的信息
	private Page page = new Page();	//分页信息
	private List pageList = new ArrayList(); //分页信息列表
	
	public void completeResultList(Object obj){
		UIHelper.completeResultList(pageList, obj, page);
	}
	
	public Page getPage() {
		return page;
	}

	public void setPage(Page page) {
		this.page = page;
	}

	public List getPageList() {
		return pageList;
	}

	public void setPageList(List pageList) {
		this.pageList = pageList;
	}

	public void outJsonString(String str) {
		getResponse().setContentType("text/javascript;charset=UTF-8");
		outString(str);
	}

	public void outString(String str) {
		try {
			PrintWriter out = getResponse().getWriter();
			out.write(str);
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	public void outXMLString(String xmlStr) {
		getResponse().setContentType("application/xml;charset=UTF-8");
		outString(xmlStr);
	}

	/**
	 * 获得request
	 * 
	 * @return
	 */
	public HttpServletRequest getRequest() {
		return ServletActionContext.getRequest();
	}

	/**
	 * 获得response
	 * 
	 * @return
	 */
	public HttpServletResponse getResponse() {
		return ServletActionContext.getResponse();
	}

	/**
	 * 获得session
	 * 
	 * @return
	 */
	public HttpSession getSession() {
		return getRequest().getSession();
	}

	/**
	 * 获得servlet上下文
	 * 
	 * @return
	 */
	public ServletContext getServletContext() {
		return ServletActionContext.getServletContext();
	}

	public String getRealyPath(String path) {
		return getServletContext().getRealPath(path);
	}

	public String getMsg() {
		return msg;
	}

	public void alert(String msg) {
		this.msg = msg;
	}

	public void request(Object key,Object obj) {
		getRequestMsg().put(key, obj);
	}

	public Map<Object, Object> getRequestMsg() {
		return requestMsg;
	}

	public abstract Object getModel();
}
